﻿using System;
using System.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Microsoft.WindowsAzure.Accelerator
{
    /// <summary>
    /// General extension methods for Windows Azure resources.
    /// </summary>
    public static class AzureExtensions
    {
        /// <summary>
        /// Gets the config setting of the specified type; otherwise returns the default value.
        /// </summary>
        /// <param name="settingName">Name of the configuration setting.</param>
        /// <param name="defaultValue">Default value to return if the setting is not found or available.</param>
        /// <returns>Configuration setting value.</returns>
        public static T GetRoleSetting<T>(String settingName, T defaultValue) where T : IConvertible
        {
            if (RoleEnvironment.IsAvailable)
            {
                try {
                    return RoleEnvironment.GetConfigurationSettingValue(settingName).As<T>();
                }
                catch {
                    Trace.TraceError("Unable to load configuration setting '{0}', using default '{1}'.", settingName, defaultValue);
                }
            }
            return defaultValue;
        }
    }
}
